package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.read;

import java.util.ArrayList;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtil;

public class ReadParam {
  private byte valueNum;
  private byte[] retain;
  private byte[] valueId;
  private byte[] readStrValues;

  public ReadParam(byte[] valueId, List<ReadStrValue> rvalues) {
    // 这里的valueId 也是 header里面的 transationId
    valueNum = ByteUtil.intTo1Byte(rvalues.size());
    retain = new byte[]{0x00, 0x00, 0x00};

    this.valueId = new byte[]{valueId[0], valueId[1], 0x00, 0x00};
    List<Byte> bytes = new ArrayList<>();
    for (ReadStrValue b : rvalues) {
      bytes.addAll(b.toBytes());
    }
    readStrValues = new byte[bytes.size()];
    for (int i = 0; i < bytes.size(); i++) {
      readStrValues[i] = bytes.get(i);
    }
  }

  public byte[] toBytes() {
    List<Byte> bytes = new ArrayList<>();
    bytes.add(valueNum);
    for (byte b : retain) {
      bytes.add(b);
    }
    for (byte b : valueId) {
      bytes.add(b);
    }
    for (byte b : readStrValues) {
      bytes.add(b);
    }
    byte[] b = new byte[bytes.size()];
    for (int i = 0; i < bytes.size(); i++) {
      b[i] = bytes.get(i);
    }
    return b;
  }
}
